package com.labyrinth.core;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.StringReader;

@SuppressWarnings({"UnusedDeclaration", "UnusedDeclaration", "UnusedDeclaration", "UnusedDeclaration", "UnusedDeclaration", "UnusedDeclaration", "UnusedDeclaration", "UnusedDeclaration", "UnusedDeclaration"})
public class Level
{
	private int row;
	private int col;
	private int[][] intLevel;
	private GameObject[][] brickLevel;
	private int mazeYourDirection;
	private int yourDirection;
	private int mazeEnemyDirection;
	private int enemyDirection;
	private int mazeTime;
	private int time;
	private int mazeMoves;
	private int moves;
    public float startX;
	public float startY;
	public float exitX;
	public float exitY;
	public int startu;
	public int starti;
	public int maxLevels;
	public Player player;

	public void changeLevel()
	{
		yourDirection = mazeYourDirection;
		enemyDirection = mazeEnemyDirection;
		time = mazeTime;
		moves = mazeMoves;

        int menu = 10;
        float height = (800 / row) - menu;
		float width = (480) / col;
		for (int i = row - 1; i >= 0; i--)
		{
			for (int u = 0; u < col; u++)
			{
				float x = width / 2 + (u * width);
				float y = 725 - height / 2 - ((row - i - 1) * height);
				switch (intLevel[i][u])
				{
					case 0:
					{
						brickLevel[i][u] = new Floor(x, y, height, width);
						break;
					}
					case 1:
					{
						brickLevel[i][u] = new MovableBrick(x, y, height, width, 1);
						break;
					}
					case 2:
					{
						brickLevel[i][u] = new MovableBrick(x, y, height, width, 2);
						break;
					}
					case 3:
					{
						brickLevel[i][u] = new MovableBrick(x, y, height, width, 3);
						break;
					}
					case 4:
					{
						brickLevel[i][u] = new MovableBrick(x, y, height, width, 4);
						break;
					}
					case 5:
					{
						brickLevel[i][u] = new WildcardBrick(x, y, height, width, 1);
						break;
					}
					case 6:
					{
						brickLevel[i][u] = new Trap(x, y, height, width);
						break;
					}
					case 7:
					{
						brickLevel[i][u] = new Exit(x, y, height, width);
						exitX = x;
						exitY = y;
						break;
					}
					case 8:
					{
						brickLevel[i][u] = new Floor(x, y, height, width);
						player = new Player(x, y, height / 2, width / 2, yourDirection);
						startX = x;
						startY = y;
						startu = u;
						starti = i;
						break;
					}
					case 9:
					{
						if (enemyDirection != 0)
						{
							//brickLevel[i][u] = new Enemy(x, y, height, width, enemyDirection);
						}
						else
						{
							brickLevel[i][u] = null;
						}
						break;
					}
				}

			}
		}
	}
	
	
	public void ReadLevel(int levelnum)
	{
		FileHandle file  = (Gdx.files.internal("data/Levels.txt"));
		String fileContent = file.readString();
		BufferedReader reader = new BufferedReader	(new StringReader(fileContent));
		try
		{

			String text = null;

			// repeat until all lines is read
			while ((text = reader.readLine()) != null) 
			{
				if ((text.contains("Mazes")))
				{
					maxLevels = Integer.parseInt(text.split(" ")[1]);
				}
				if (text.contentEquals("L " + levelnum))
				{
					text = reader.readLine();
					row = Integer.parseInt(text.split(" ")[0]);
					col = Integer.parseInt(text.split(" ")[1]);
					intLevel = new int[row][col];
					brickLevel = new GameObject[row][col];
					for (int i = row - 1; i >= 0; i--)
					{
						text = reader.readLine();
						for (int u = 0; u < col; u++)
						{
							intLevel[i][u] = Integer.parseInt(text.split(" ")[u]);
						}
					}
					text = reader.readLine();
					mazeYourDirection = Integer.parseInt(text);
					text = reader.readLine();
					mazeEnemyDirection = Integer.parseInt(text);
					text = reader.readLine();
					mazeTime = Integer.parseInt(text);
					text = reader.readLine();
					mazeMoves = Integer.parseInt(text);
					break;
				}
			}
		}
		catch (FileNotFoundException e)
		{
			e.printStackTrace();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				if (reader != null)
				{
					reader.close();
				}
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
		}
		changeLevel();
	}

	public int[][] getIntLevel()
	{
		return intLevel;
	}

	public GameObject[][] getBrickLevel()
	{
		return brickLevel;
	}

	public int getRow()
	{
		return row;
	}

	public int getCol()
	{
		return col;
	}

	public int getYourdirection()
	{
		return yourDirection;
	}

	public int getEnemydirection()
	{
		return enemyDirection;
	}

	public int getTime()
	{
		return time;
	}

	public int getMoves()
	{
		return moves;
	}

}
